package com.kun.controller;

import com.kun.annotation.MethodPermissionCheck;
import com.kun.common.domain.AjaxResult;
import com.kun.domain.TUserInfo;
import com.kun.service.service.ITUserInfoService;
import com.kun.service.service.ITUserService;
import com.kun.domain.TUser;
import com.kun.service.util.UserContext;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/t-users")
public class TUserController extends BaseController {

    private final ITUserService itUserService;

    private final ITUserInfoService itUserInfoService;

    public TUserController(ITUserService itUserService, ITUserInfoService itUserInfoService) {
        this.itUserService = itUserService;
        this.itUserInfoService = itUserInfoService;
    }

    /**
     * 添加用户
     */
    @PostMapping
    public ResponseEntity<AjaxResult> addUser(@Validated @RequestBody TUser tUser) {
        return success(itUserService.addUser(tUser));
    }

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public ResponseEntity<AjaxResult> login(@Validated @RequestBody TUser tUser) {
        return success(itUserService.login(tUser));
    }

    /**
     * 用户退出
     */
    @GetMapping("/logout")
    public ResponseEntity<AjaxResult> logout() {
        itUserService.logout();
        return success(true);
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/info")
    public ResponseEntity<AjaxResult> getInfo() {
        return success(itUserService.getInfo());
    }

    /**
     * 修改用户密码手机号邮箱
     */
    @PutMapping
    public ResponseEntity<AjaxResult> updateUser(@Validated @RequestBody TUser tUser) {
        return success(itUserService.updateUser(tUser));
    }

    /**
     * 修改用户信息
     */
    @PutMapping("/info")
    public ResponseEntity<AjaxResult> updateUserInfo(@Validated @RequestBody TUserInfo tUserInfo) {
        return success(itUserInfoService.updateUserInfo(tUserInfo));
    }

    /**
     * 获取当前用户权限信息
     */
    @GetMapping("/user-permission")
    public ResponseEntity<AjaxResult> queryPermissionByUserId() {
        return success(UserContext.getUserPermission());
    }


}
